/* DISCLAIMER: This is a PROTOTYPE and must NOT be used to plan real dives.
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ics414scuba;

import java.awt.Image;
import java.awt.Point;
import java.util.ArrayList;
import org.netbeans.api.visual.action.ActionFactory;
import org.netbeans.api.visual.action.WidgetAction;
import org.netbeans.api.visual.anchor.AnchorFactory;
import org.netbeans.api.visual.widget.ConnectionWidget;
import org.netbeans.api.visual.widget.ImageWidget;
import org.netbeans.api.visual.widget.LabelWidget;
import org.netbeans.api.visual.widget.LayerWidget;
import org.netbeans.api.visual.widget.Scene;
import org.netbeans.api.visual.widget.Widget;
import org.netbeans.api.visual.widget.general.IconNodeWidget;
import org.openide.util.ImageUtilities;

/**
 * DISCLAIMER: This is a PROTOTYPE and must NOT be used to plan real dives.
 * @author Elliot Ito
 */
public class GraphInterface extends Scene{
     private static final Image IMAGE = ImageUtilities.loadImage ("resources/cartoon-scuba-diver-hi.png"); // NOI18N
     private static final Image BOAT = ImageUtilities.loadImage ("resources/boat.png"); // NOI18N
     private Widget previousNode;
     private LabelWidget position;
     private LabelWidget position2;
     private LayerWidget mainLayer; 
     private LayerWidget connLayer;
     private ImageWidget previousIcon;
     private ImageWidget[] dives;
     private ImageWidget[] breaks;
     private ArrayList<LabelWidget> positions;
     private ArrayList<ConnectionWidget> connections;
     private int nodeCount = 0;
     private int totalTime = 0;
     
     /**
      * DISCLAIMER: This is a PROTOTYPE and must NOT be used to plan real dives.
      */
        public GraphInterface(){
          positions = new ArrayList<>();
          mainLayer = new LayerWidget (this);
          connLayer = new LayerWidget (this);
          addChild (mainLayer);
          addChild (connLayer);
        }
        
    /** DISCLAIMER: This is a PROTOTYPE and must NOT be used to plan real dives.
     *
     * @param depth
     * @param diveTime
     */
    public void makeDive(int depth, int diveTime){
            position2 = new LabelWidget (this);
            Widget node1 = attachNodeWidget(null,null,new Point (0, 100),BOAT);
            //ImageWidget image1 = new ImageWidget (this, BOAT);
            //image1.setPreferredLocation (new Point (0, 100));
            //mainLayer.addChild (image1);
            Widget node2 = attachNodeWidget(null,null,new Point (150, 100),BOAT);
//            ImageWidget image2 = new ImageWidget (this, BOAT);
//            image2.setPreferredLocation (new Point (diveTime+150, 100));
//            mainLayer.addChild (image2);
            Widget node3 = attachNodeWidget("Depth: " + depth + " Time: " + diveTime,null,new Point (diveTime, depth+250),IMAGE);
//            floatingImage = new ImageWidget (this, IMAGE);
//            mainLayer.addChild(position2);
//            position2.setPreferredLocation(new Point(diveTime, depth+240));
//            floatingImage.setPreferredLocation (new Point (diveTime, depth+250));
//            position2.setLabel ("Depth: " + depth + " Time: " + diveTime);
//            mainLayer.addChild (floatingImage); 
//            floatingImage.getActions().addAction (new PositionAction ());
            ConnectionWidget conn1 = new ConnectionWidget (this);
            conn1.setSourceAnchor (AnchorFactory.createRectangularAnchor (node1));
            conn1.setTargetAnchor (AnchorFactory.createRectangularAnchor (node3));
            connLayer.addChild (conn1);
            ConnectionWidget conn2 = new ConnectionWidget (this);
            conn2.setSourceAnchor (AnchorFactory.createRectangularAnchor (node3));
            conn2.setTargetAnchor (AnchorFactory.createRectangularAnchor (node2));
            connLayer.addChild (conn2);
            previousNode = node2;
            totalTime += diveTime;
            this.validate();
        }
     
    protected Widget attachNodeWidget(String node, String description, Point point, Image image) {
        IconNodeWidget label = new IconNodeWidget(this);
        this.nodeCount++;
        int temp, temp2;
        int pointX = 0;
        if(this.nodeCount < 4){
        }
        else if(this.nodeCount%2 == 0){
            temp = 100 * this.nodeCount+1;
            temp2 = 100 * (this.nodeCount - 1);
            pointX = (temp + temp2)/2;
            point.x = pointX;
        }
        else{
        }
        label.setToolTipText(description);
        label.setLabel(node);
        label.setImage(image);
        label.setPreferredLocation(point);
        mainLayer.addChild(label);
        this.validate();
        System.out.println(this.nodeCount);
        System.out.println(point);
        return label;
    }
    
    
    /** DISCLAIMER: This is a PROTOTYPE and must NOT be used to plan real dives.
     *
     * @return
     */
    public ImageWidget getCurrentBreak(){
            return this.previousIcon;
        }
        
    /** DISCLAIMER: This is a PROTOTYPE and must NOT be used to plan real dives.
     *
     * @param depth
     * @param diveTime
     * @param breakTime
     */
    public void makeRepetiveDive(int depth, int diveTime, int breakTime){
            totalTime += diveTime; 
            int extra = this.nodeCount*100;
            int extra1 = (this.nodeCount - 1)*100;
            int extra2 = (this.nodeCount + 1)*100;
            int boatX = (extra1+extra2)/2;
            Widget node2 = attachNodeWidget(null,null,new Point (extra, 100),BOAT);
            Widget node3 = attachNodeWidget("Depth: " + depth + " Time: " + diveTime,null,new Point (boatX, depth+250),IMAGE);
           
            
            ConnectionWidget conn1 = new ConnectionWidget (this);
            conn1.setSourceAnchor (AnchorFactory.createRectangularAnchor (previousNode));
            conn1.setTargetAnchor (AnchorFactory.createRectangularAnchor (node3));
            connLayer.addChild (conn1);
            ConnectionWidget conn2 = new ConnectionWidget (this);
            conn2.setSourceAnchor (AnchorFactory.createRectangularAnchor (node3));
            conn2.setTargetAnchor (AnchorFactory.createRectangularAnchor (node2));
            connLayer.addChild (conn2);
            this.validate();
            previousNode = node2;
            
            
//            ImageWidget image3 = new ImageWidget (this, BOAT);
//            image3.setPreferredLocation (new Point (diveTime+150, 100));
//            mainLayer.addChild (image3);
            
//            ImageWidget floatingImage2 = new ImageWidget (this, IMAGE);
//            LabelWidget positionNew = new LabelWidget(this);
//            mainLayer.addChild(positionNew);

//            positionNew.setPreferredLocation(new Point(diveTime, depth+240));
//            floatingImage2.setPreferredLocation (new Point (diveTime, depth+250));
//            positionNew.setLabel ("Depth: " + depth + " Time: " + diveTime);
//            mainLayer.addChild (floatingImage2); 
//            floatingImage2.getActions().addAction (new PositionAction ());
//            //positions.add(positionNew);
//            mainLayer.addChild (floatingImage2); 

//            ConnectionWidget conn1 = new ConnectionWidget (this);
//            conn1.setSourceAnchor (AnchorFactory.createRectangularAnchor (previousIcon));
//            conn1.setTargetAnchor (AnchorFactory.createRectangularAnchor (floatingImage2));
//            connLayer.addChild (conn1);
//
//            ConnectionWidget conn2 = new ConnectionWidget (this);
//            conn2.setSourceAnchor (AnchorFactory.createRectangularAnchor (floatingImage2));
//            conn2.setTargetAnchor (AnchorFactory.createRectangularAnchor (image3));
//            connLayer.addChild (conn2);     
//            previousIcon = image3;
        }

//        private class PositionAction extends WidgetAction.Adapter {
//            @Override
//            public WidgetAction.State mouseMoved (Widget widget, WidgetAction.WidgetMouseEvent event) {
//                //loop though all dives
//                Point point2 = floatingImage.getLocation();
//                position2.setPreferredLocation(new Point(point2.x, point2.y-10));
//                return WidgetAction.State.REJECTED; 
//            }
//        }
}
